from kubernetes import client, config

def get_cluster_overview():
    # 加载 kubeconfig 文件，如果没有指定，则使用默认的 kubeconfig 文件
    config.load_kube_config()

    # 创建 CoreV1Api 的实例，用于与集群通信
    v1 = client.CoreV1Api()

    # 获取集群节点数量
    nodes = v1.list_node().items
    node_count = len(nodes)

    # 获取命名空间数量
    namespaces = v1.list_namespace().items
    namespace_count = len(namespaces)

    # 获取所有 Pod 的数量
    pods = v1.list_pod_for_all_namespaces().items
    pod_count = len(pods)

    # 获取所有 Service 的数量
    services = v1.list_service_for_all_namespaces().items
    service_count = len(services)

    # 获取所有 PersistentVolume 的数量
    pv = v1.list_persistent_volume().items
    pv_count = len(pv)

    # 获取所有事件的数量
    events = v1.list_event_for_all_namespaces().items
    event_count = len(events)

    # 获取集群的名称和版本
    # config_map = v1.read_namespaced_config_map("kube-system", "cluster-info")
    # cluster_name = config_map.data["cluster-name"]
    # cluster_version = config_map.data["cluster-version"]

    # # 假设集群状态为运行中
    # cluster_status = "Running"

    return {
        # "cluster_name": cluster_name,
        # "cluster_version": cluster_version,
        # "cluster_status": cluster_status,
        "node_count": node_count,
        "namespace_count": namespace_count,
        "pod_count": pod_count,
        "service_count": service_count,
        "pv_count": pv_count,
        "event_count": event_count
    }

# 测试代码
if __name__ == "__main__":
    cluster_info = get_cluster_overview()
    print(cluster_info)
